package com.example.guiguiapplicatio.fragment.Fulfillment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.graphics.Color;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.api.GetdataResponseBody.GetfulfillmentServiceApi;
import com.example.guiguiapplicatio.DataManager.GetfulfillmentServiceData;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.ServicePackServiceitems;
import com.example.guiguiapplicatio.Response.Serviceitems;
import com.example.guiguiapplicatio.fragment.Fulfillment.adapter.FulfillmenthomeAdapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class FulfillmenthomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private FulfillmenthomeAdapter fulfillmenthomeAdapter;
    List<ServicePackServiceitems> servicePackageitemsList = new ArrayList<>();
    private List<Serviceitems> serviceitemsList1 = new ArrayList<>();;
    private GetfulfillmentServiceData getfulfillmentServiceData;
    private GetfulfillmentServiceApi getfulfillmentServiceApi;
    private String idCard;
    Button allButton ;
    Button fulfilledButton ;
    Button nonPerformanceButton ;
    ImageButton servic_back;
    private int Filtervalues = 1;

    public FulfillmenthomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HealthRecordFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FulfillmenthomeFragment newInstance(String param1, String param2) {
        FulfillmenthomeFragment fragment = new FulfillmenthomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            idCard = getArguments().getString("idCard");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_fulfillment, container, false);
        allButton = root.findViewById(R.id.all);
        servic_back = root.findViewById(R.id.servic_back);
        fulfilledButton = root.findViewById(R.id.Fulfilled);
        nonPerformanceButton = root.findViewById(R.id.Non_performance);
        updateButtonState(Filtervalues);

// 设置按钮的点击事件监听器
        allButton.setOnClickListener(v -> {
            // 更新 Filtervalues 为 1
            Filtervalues = 1;
            // 更新按钮状态
            getfulfillmentServiceApi.performgetfulfillmentService(idCard,Filtervalues);
            updateButtonState(Filtervalues);
        });

        fulfilledButton.setOnClickListener(v -> {
            // 更新 Filtervalues 为 2
            Filtervalues = 2;
            getfulfillmentServiceApi.performgetfulfillmentService(idCard,Filtervalues);
            // 更新按钮状态
            updateButtonState(Filtervalues);
        });

        nonPerformanceButton.setOnClickListener(v -> {
            // 更新 Filtervalues 为 0
            Filtervalues = 0;
            // 更新按钮状态
            getfulfillmentServiceApi.performgetfulfillmentService(idCard,Filtervalues);
            updateButtonState(Filtervalues);
        });
        RecyclerView recyclerView = root.findViewById(R.id.service_list);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        fulfillmenthomeAdapter = new FulfillmenthomeAdapter(new ArrayList<>(),getContext());
        recyclerView.setAdapter(fulfillmenthomeAdapter);
        getfulfillmentServiceData = new GetfulfillmentServiceData(fulfillmenthomeAdapter,R.layout.fragment_fulfillment);
        getfulfillmentServiceApi = new GetfulfillmentServiceApi();
        getfulfillmentServiceApi.setCallback(getfulfillmentServiceData);
        getfulfillmentServiceApi.performgetfulfillmentService(idCard,Filtervalues);
        servic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                Navigation.findNavController(v).navigate(R.id.action_FulfillmenthomeFragment_to_recordDetailFragment, bundle);
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }
    private void updateButtonState(int filterValue) {
        switch (filterValue) {
            case 0:
                // 当 Filtervalues 为 0 时，设置 Non_performance 按钮的背景颜色为蓝色，其他按钮恢复原来颜色
                nonPerformanceButton.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE));
                allButton.setBackgroundTintList(null); // 恢复原来颜色
                fulfilledButton.setBackgroundTintList(null); // 恢复原来颜色
                break;
            case 1:
                // 当 Filtervalues 为 1 时，设置 All 按钮的背景颜色为蓝色，其他按钮恢复原来颜色
                allButton.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE));
                fulfilledButton.setBackgroundTintList(null); // 恢复原来颜色
                nonPerformanceButton.setBackgroundTintList(null); // 恢复原来颜色
                break;
            case 2:
                // 当 Filtervalues 为 2 时，设置 Fulfilled 按钮的背景颜色为蓝色，其他按钮恢复原来颜色
                fulfilledButton.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE));
                allButton.setBackgroundTintList(null); // 恢复原来颜色
                nonPerformanceButton.setBackgroundTintList(null); // 恢复原来颜色
                break;
        }
    }

}
